﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class Modules_Personal_ModTargetAdd : System.Web.UI.Page
{
    IPerf_Personal_TargetHandler targetHandler = new Perf_Personal_TargetManage();
    IPerf_Personal_TargetTypeHandler ttHandler = new Perf_Personal_TargetTypeManage();
    IPerf_Personal_FinalAssignDtlHandler finalAssingDtlHandler = new Perf_Personal_FinalAssignDtlManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckPurviews())
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!IsPostBack)
        {
            dataBind();
        }
    }

    private bool CheckPurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            string strPur = Session["UserPurviews"].ToString();
            if (strPur.Contains("301040"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    private void dataBind()
    {
        FillTargetType();
    }

    private void FillTargetType()
    {
        DataTable dtTargetType = ttHandler.GetPerf_Personal_TargetTypeList(" Status = @Status", new SqlParameter[] { new SqlParameter("@Status", (int)Enums.NormalStatus.Vallid) }, " id asc");
        ddlTargetType.DataTextField = "Name";
        ddlTargetType.DataValueField = "ID";
        ddlTargetType.DataSource = dtTargetType;
        ddlTargetType.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if ("".Equals(txtCode.Text.Trim()))
        {
            Jscript.Alert("代码不能为空", this.Page);
            return;
        }
        if ("".Equals(txtName.Text.Trim()))
        {
            Jscript.Alert("名称不能为空", this.Page);
            return;
        }
        if ("".Equals(txtScore.Text.Trim()))
        {
            Jscript.Alert("分值不能为空", this.Page);
            return;
        }

        int targetId = 0;
        Perf_Personal_TargetQuery query = new Perf_Personal_TargetQuery();
        
        query.TargetOwner_ID = 0;
        query.Code = txtCode.Text.Trim();
        query.Name = txtName.Text.Trim();
        query.TargetType_ID = int.Parse(ddlTargetType.SelectedValue);
        query.Score = float.Parse(txtScore.Text.Trim());
        query.Status = (int)Enums.NormalStatus.Vallid;
        query.IsRequired = (int)Enums.YesNoStatus.No;
        query.User_ID = int.Parse(Session["CurrentUserID"].ToString());
        query.Remark = txtRemark.Text;
        query.Create_Date = DateTime.Now;

        targetHandler.InsertPerf_Personal_Target(query, ref targetId);
        if (targetId != 0)
        {
            int assignId = int.Parse(Request["assignId"]);

            Perf_Personal_FinalAssignDtlQuery dtlQuery = new Perf_Personal_FinalAssignDtlQuery();
            dtlQuery.Final_Assign_ID = assignId;
            dtlQuery.Target_ID = targetId;
            finalAssingDtlHandler.InsertPerf_Personal_FinalAssignDtl(dtlQuery);

            Jscript.Alert("添加成功！", this.Page);
            Jscript.ReturnAndClose(this.Page, "true");
        }
        else
        {
            Jscript.Alert("添加失败", this.Page);
        }
    }

}
